<?php

class SettingsController extends BackendController
{
    protected $__model;

    public function actionIndex()
    {
        $model=$this->model;

        $criteria=new CDbCriteria();
        $sort=new CSort();
        $sort->sortVar='sort';
        $sort->defaultOrder='id ASC';
        $sort->multiSort=true;
        /*
        $sort->attributes = array(
            'id'=>array(
                'label'=>'id',
                'asc'=>'product_id ASC',
                'desc'=>'product_id DESC',
                'default'=>'desc',
            ),
            'model'=>array(
                'label'=>'цене',
                'asc'=>'model ASC',
                'desc'=>'model DESC',
                'default'=>'desc',
            ),
            'quantity'=>array(
                'label'=>'количеству',
                'asc'=>'quantity ASC',
                'desc'=>'quantity DESC',
                'default'=>'desc',
            ),
            'price'=>array(
                'label'=>'цене',
                'asc'=>'price ASC',
                'desc'=>'price DESC',
                'default'=>'desc',
            ),
        );
        */

        $dataProvider=new CActiveDataProvider($model,array(
                'criteria'=>$criteria,
                'sort'=>$sort,
                'pagination'=>array(
                    'pageSize'=>$model->getConfig('elements_per_page_admin'),
                ),
            )
        );
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }

    public function actionView()
    {
        $model=$this->model->findByPk(Yii::app()->request->getQuery('id'));

        if(Yii::app()->request->isAjaxRequest){
            $this->renderPartial('admin.views.settings.__setting',array(
               'model'=>$model,
            ));
        }else{
            $this->render('view',array(
                'model'=>$model,
            ));
        }
    }

    public function actionChange()
    {
        $model=$this->model;
        $this->performAjaxValidation($model);

        if(isset($_POST['Setting'])){
            $params=$_POST['Setting'];

            //Test::show($params);

            $id=isset($params['id']) ? $params['id'] : 0;

            if($id) $model=$model->findByPk($id);

            if($model->attr=$params) $model->save();

            if($errors=$model->errors){
                Yii::app()->notification->setError($errors);
                if($id) $this->redirect(Yii::app()->createUrl('settings/view',array('id'=>$id)));
            }
        }
        $this->redirect($this->createUrl('settings/index'));
    }

    public function actionCreate()
    {
        if(!Yii::app()->request->isAjaxRequest || !Yii::app()->request->getQuery('create')) $this->redirect(Yii::app()->createUrl('settings'));
        $model=$this->model;

        $json=array(
            'htmlContent'=>'',
            'message'=>'',
            'error'=>'',
        );

        $json['htmlContent']=$this->renderPartial('view',array(
            'model'=>$model,
        ),true,false);

        Yii::app()->end(json_encode($json));
    }

    public function actionDelete($id)
    {
        if($id){
            $transaction=Yii::app()->db->beginTransaction();

            $model=Setting::model()->findByPk($id);
            $name=$model->name;

            try{
                $dropDownModels=$model->dropDownModels;

                if($dropDownModels && is_array($dropDownModels)){
                    foreach($dropDownModels as $dropDownModel){
                        $multiLangModels=$dropDownModel->getMultiLangModels(true);
                        if($multiLangModels && is_array($multiLangModels)){
                            foreach($multiLangModels as $multiLanguageModel){
                                $multiLanguageModel->delete();
                            }
                        }
                        $dropDownModel->delete();
                    }
                }

                $multiLangModels=$model->getMultiLangModels(true);

                if($multiLangModels){
                    foreach($multiLangModels as $lang){
                        $lang->delete();
                    }
                    unset($multiLangModels);
                }

                $model->delete();

                $transaction->commit();

                Yii::app()->notification->message=Yii::t('settings','settingRemoved',array('{setting}'=>$name));
            }catch(Exception $e){
                $transaction->rollback();
                Yii::app()->notification->error=Yii::t('settings','settingNoRemoved',array('{setting}'=>$name)).$e->getMessage();
            }
        }

        $this->redirect($this->createUrl('settings/index'));
    }

    public function actionChangeType()
    {
        $type=Yii::app()->request->getQuery('type');

        if(!Yii::app()->request->isAjaxRequest || !$type) $this->redirect($this->createUrl('settings/index'));
        $model=$this->model;
        $model->type=$type;

        $json=array(
            'htmlContent'=>'',
            'message'=>'',
            'error'=>'',
        );

        $json['htmlContent']=$this->renderPartial('view',array(
            'model'=>$model,
        ),true,false);

        Yii::app()->end(json_encode($json));
    }

    public function actionDeleteDropDownField()
    {
        $id=Yii::app()->request->getQuery('id');
        if(!Yii::app()->request->isAjaxRequest || !$id) $this->redirect($this->createUrl('settings/index'));

        $json=array(
            'htmlContent'=>'',
            'message'=>'',
            'error'=>'',
        );

        if($model=SettingDropDown::model()->findByAttributes(array('id'=>$id))) $model->delete();

        Yii::app()->end(json_encode($json));
    }

    public function getModel()
    {
        if($this->__model===null) $this->__model=new Setting();
        return $this->__model;
    }

    protected function setModel($model)
    {
        $this->__model=$model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='setting-form'){
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}